﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTTB.Assembler;
using DTTB.DAO;
using DTTB.Domain;
using DTTB.DTO;

namespace DTTB.SERVICE
{
    public class RolesService
    {
        RolesAssembler RolesAssembler = new RolesAssembler();
        RolesInPathsAssembler RolesInPathsAssembler = new RolesInPathsAssembler();
        RolesDao RolesDao = new RolesDao();
        RolesInPathsDao RolesInPathsDao = new RolesInPathsDao();

        public IList<RolesDto> GetAllRoles()
        {
            IList<Roles> rolesList = RolesDao.GetAllRoles();

            IList<RolesDto> rolesDtoList = RolesAssembler.ConvertToDataTransferObjectList(rolesList);

            return rolesDtoList;
        }

        public IList<RolesDto> GetAllRolesIsEnable()
        {
            IList<Roles> rolesList = RolesDao.GetAllRolesIsEnable();

            IList<RolesDto> rolesDtoList = RolesAssembler.ConvertToDataTransferObjectList(rolesList);

            return rolesDtoList;
        }

        public bool AddNewRoles(RolesDto roles)
        {
            return RolesDao.AddNewRoles(RolesAssembler.ConvertToBusinessObject(roles));
        }

        public bool Update(RolesDto rolesDto)
        {
            return RolesDao.Update(RolesAssembler.ConvertToBusinessObject(rolesDto));
        }

        public bool DeleteRoles(string ID)
        {
            return RolesDao.DeleteRoles(ID);
        }

        public RolesDto GetRolesByID(string ID)
        {
            RolesDto RolesDto = RolesAssembler.ConvertToDataTransferObject(RolesDao.GetRolesByID(ID));

            return RolesDto;
        }

        public RolesDto GetRolesByName(string name)
        {
            RolesDto rolesList = RolesAssembler.ConvertToDataTransferObject(RolesDao.GetRolesByName(name));

            return rolesList;
        }

        public RolesDto GetRolesDeletedByName(string name)
        {
            RolesDto rolesList = RolesAssembler.ConvertToDataTransferObject(RolesDao.GetRolesDeletedByName(name));

            return rolesList;
        }

        public bool ShiftDelete(string roleID)
        {
            return RolesDao.ShiftDelete(roleID);
        }

        //[Transaction] : cần cài đặt
        public void UpdateManageRolesWithPages(IList<RolesInPathsDto> rolesInPathsDtos, string roleId)
        {
            ////Delete all RolesInPaths for existing Roles
            this.RolesInPathsDao.DeleteRolesInPathsByRolesId(roleId);

            ////update new setting
            foreach (var dto in rolesInPathsDtos)
            {
                this.RolesInPathsDao.AddNewRolesInPaths(this.RolesInPathsAssembler.ConvertToBusinessObject(dto));
            }
        }
    }
}
